
import { setStorage, getStorage } from "@/baseFnMixin/localStorage/storageMixin.js";
let storage = null;
try {
    if (window.localStorage.getItem("songList") !== null) {
        storage = JSON.parse(getStorage(window.localStorage.getItem("songList")))
    }

}
catch (err) {
    window.alert("不建议修改存储的值")
}

export default {
    state: () => ({
        songlist: storage,
        //  控制歌词列表隐藏与显示功能
        flag: false,
        tipFlag: false,

        tipString: null,
    }),
    actions: {

    },

    mutations: {
        songlistAdd: function (state, value) {
            if (state.songlist === null) {

                if (Object.prototype.toString.call(value) === "[object Object]") {
                    state.songlist = [value];
                } else {
                    state.songlist = value
                }
                const data = setStorage(JSON.stringify(state.songlist));
                window.localStorage.setItem("songList", data);


            } else {

                if (Object.prototype.toString.call(value) === "[object Object]") {

                    for (let index = 0; index < state.songlist.length; index++) {
                        
                        if (state.songlist[index].id === value.id) {
                            break;
                        }
                        if (index === state.songlist.length - 1) {
                            state.songlist.push(value);
                        }
                    }

                } else {
                    // 去重添加
                    let arr = state.songlist;
                    value.forEach((el) => {
                        for (let index = 0; index < arr.length; index++) {
                            if (el.id === arr[index].id) { break; }
                            if (index === arr.length - 1) {
                                //  添加
                                state.songlist.push(el);
                            }
                        }
                    })
                }
                const data = setStorage(JSON.stringify(state.songlist));
                window.localStorage.setItem("songList", data);
            }

        },
        changFlag(state, value) {
            state.flag = value;
        },
        //  删除歌词列表中的歌曲
        SONGLISTDELETE(state, value) {
            state.songlist.splice(value, 1);
            const data = setStorage(JSON.stringify(state.songlist));
            window.localStorage.setItem("songList", data);
        },
        changeSonglistTip(state, value) {
            state.tipFlag = value.flag;
            state.tipString = value.tipString;

        }
    }
}
// window.localStorage.removeItem("songList");


